//
//  CZAriginalView.swift
//  SinaBlog
//
//  Created by 李超 on 16/3/25.
//  Copyright © 2016年 lichao. All rights reserved.
//

import UIKit
import SnapKit

class CZOriginalView: UIView {

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
   override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor.whiteColor()
    //添加子控件
        prepareUI()
    
    }
    //statuses模型属性
    var status:CZStatuses?{
        //赋值后进行更新ui
        didSet{
            //给头像赋值
            if let urlString = status?.user?.profile_image_url{
                let url = NSURL(string: urlString)
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar_default_big"))
            }
            //昵称
            nameLable.text = status?.user?.screen_name
            //时间
            timeLable.text = NSDate.sinaDateFromString(status?.created_at ?? "刚刚").sinaDateDecription()
            //来源
            sourceLable.text = status?.source
            //内容
            contentTxet.text = status?.text
            //等级图片
            if let image = status?.user?.mbrankImage{
                
                membershipView.image = image
            }else{
                
                membershipView.image = nil
            }
            
            //认证图片
            if let avataimg = status?.user?.verifiedImage{
                avatarView.image = avataimg
            }else{
                avatarView.image = nil 
                
            }
            
                        //配图
            let count = status?.pictureUrls?.count ?? 0
            //解除约束,并根据条件
            self.originalVlewBottomConstrain?.uninstall()
            if count == 0{
                pictureView.pictureUrls = nil
                pictureView.hidden = true
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.originalVlewBottomConstrain = make.bottom.equalTo(pictureView).constraint
                    
                })
            }else{
                
                pictureView.pictureUrls = status?.pictureUrls
                pictureView.hidden = false
                self.snp_makeConstraints(closure: { (make) -> Void in
                    
                    self.originalVlewBottomConstrain = make.bottom.equalTo(pictureView).offset(margin).constraint
                    
                })

                
            }
            
        }
        
    }
    //用于记录originalView的底部约束对象
    var originalVlewBottomConstrain: Constraint?
    
    
    //添加子控件，并添加布局
    func prepareUI(){
        //添加子控件
        addSubview(iconView)
        addSubview(nameLable)
        addSubview(timeLable)
        addSubview(sourceLable)
        addSubview(membershipView)
        addSubview(avatarView)
        addSubview(contentTxet)
        addSubview(pictureView)
        
        //添加布局
        //iconView的布局
        iconView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(margin)
            make.leading.equalTo(self).offset(margin)
            make.size.equalTo(CGSizeMake(35, 35))
            
        }
        //nameLable的布局
        nameLable.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconView)
           make.leading.equalTo(iconView.snp_trailing).offset(margin)
            
        }
        
        //timeLable的布局
        timeLable.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(nameLable)
            make.bottom.equalTo(iconView)
        }
        
        //sourcsLable的布局
        sourceLable.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(timeLable.snp_trailing).offset(margin)
            make.bottom.equalTo(timeLable)
            
        }
        
        //menbershipView的布局
        membershipView.snp_makeConstraints { (make) -> Void in
            
            make.centerY.equalTo(nameLable)
            make.leading.equalTo(nameLable.snp_trailing).offset(margin)
            
        }
        
        //avatarView的布局
        avatarView.snp_makeConstraints { (make) -> Void in
          
            make.centerY.equalTo(iconView.snp_bottom)
            make.centerX.equalTo(iconView.snp_trailing)
            make.size.equalTo(CGSizeMake(17, 17))
            
        }
        //contentText布局
        contentTxet.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(iconView.snp_bottom).offset(margin)
            make.leading.equalTo(iconView)
            make.trailing.equalTo(self).offset(-margin)
            
        }
        //pictureView的布局
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(self).offset(margin)
            make.top.equalTo(contentTxet.snp_bottom).offset(margin)
            make.size.equalTo(CGSizeMake(290, 290))
            
        }
        
        self.snp_makeConstraints { (make) -> Void in
           //暂时记录该约束对象
          self.originalVlewBottomConstrain = make.bottom.equalTo(pictureView).offset(margin).constraint
        }
        

    }
    /// 微博头像
    private lazy var iconView:UIImageView = UIImageView()
    /// 微博昵称
    private lazy var nameLable:UILabel = {
        
        let lable = UILabel()
        lable.font = UIFont.systemFontOfSize(14)
        return lable
    }()
    /// 微博发布时间
    private lazy var timeLable:UILabel = {
       let lable = UILabel()
        lable.font = UIFont.systemFontOfSize(11)
        return lable
        
    }()
    /// 微博发布来源
    private lazy var sourceLable:UILabel = {
      
        let lable = UILabel()
        lable.font = UIFont.systemFontOfSize(11)
        return lable
        
    }()
   /// 会员等级
    private lazy var membershipView:UIImageView = UIImageView()
  /// 认证类型
    private lazy var avatarView:UIImageView = UIImageView()
    /// 内容
    private lazy var contentTxet:UILabel = {
        let lable = UILabel()
        lable.font = UIFont.systemFontOfSize(11)
        lable.numberOfLines = 0
        return lable
        
    }()
    //原创配图
    private lazy var pictureView:CZOriginalPictureView = CZOriginalPictureView()
    
}
